<?php

namespace app\admin\controller\property;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;

/**
 * 固定资产管理
 *
 * @icon fa fa-user
 */
class Fixation extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Fixation;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order('id', 'desc')
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 查询操作
     *
     */
    public function fors(){
        $arr = request()->param();

        $data = Db::table('fa_fixation')
            ->where('id',$arr['id'])
            ->find();

        if (empty($data)){
            $this->error('数据为空');
        }else{
            $this->success('成功','',$data['name']);
        }

    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }

                $params['status'] = 1;
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validateFailException(true)->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }


    /**
     * 处置页面
     *
     */
    public function for(){

        return $this->view->fetch();
    }

    /**
     * 处置操作
     *
     */
    public function ed(){
        $arr = request()->param();


        if (request()->isPost()){
            if (empty($arr['id'])){
                $this->error('资产编号不能为空');
            }

            if (empty($arr['msg'])){
                $this->error('备注不能为空');
            }

            $update = Db::table('fa_fixation')
                ->where('id',$arr['id'])
                ->update([
                    'status' => 2,
                    'msg' => $arr['msg'],
                    'update_time' => time(),
                ]);

            if ($update !== false){
                $this->success('成功');
            }else{
                $this->error('失败');
            }


        }

    }


    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);

        if (!$row) {
            $this->error(__('No Results were found'));
        }

        $row->images = explode(',',$row->image);
        $row->invoices = explode(',',$row->invoice);
        $row->specifications = explode(',',$row->specification);

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }




}
